// pages/increase/increase.js
const util = require('../../utils/util')
// 初始化云数据库环境
let db = wx.cloud.database({
  env: 'junsheng0108-9gsv99le6b9d0ec9'
});
// 连接数据表
let job = db.collection('db_job');
let undone = db.collection('db_undone');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 过滤时间变量
    currentDate: '',
    filter(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 5 === 0);
      }
      return options;
    },
    // 时间状态值
    isOpen: false,
    // 日期状态值
    isShow: false,
    // 日期变量
    date: '',
    // 展开状态值
    show: false,
    // 任务名称变量
    job: '',
    // 任务描述变量
    detail: ''
  },
  // 获取日期
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  // 获取任务名称
  getJobName(event) {
    this.setData({
      job: event.detail,
    })
  },
  // 获取任务描述
  getDetail(event) {
    this.setData({
      detail: event.detail,
    })
  },
  // 改变时间状态值
  addInput() {
    this.setData({
      isOpen: true
    })
  },
  addTime() {
    this.setData({
      isOpen: false
    })
  },
  // 改变日期状态值
  addDate() {
    this.setData({
      isShow: true,
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
    console.log(this.data.date);
  },

  // 插入job接口，添加数据
  addJob(job_id, job_name, job_time, job_date, job_detail, job_isShow, job_flag, job_delete) {
    return new Promise((resolve, reject) => {
      job.add({
          data: {
            job_id,
            job_name,
            job_time,
            job_date,
            job_detail,
            job_isShow,
            job_flag,
            job_delete
          }
        })
        .then(content => {
          resolve(content)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 插入未完成接口
  addUndone(undone_id, undone_name, undone_time, undone_date, undone_detail) {
    return new Promise((resolve, reject) => {
      undone.add({
          data: {
            undone_id,
            undone_name,
            undone_time,
            undone_date,
            undone_detail,
          }
        })
        .then(content => {
          resolve(content)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  //点击按钮获取任务，时间，日期，任务描叙
  async addJobFunc() {
   
    let job_id = Math.random().toString().slice(2);
    let undone_id = job_id
    let job_name = this.data.job.value;
    let undone_name = this.data.job.value;
    let job_time = this.data.currentDate;
    let undone_time = this.data.currentDate;
    let job_date = this.data.date;
    let undone_date = this.data.date;
    let job_detail = this.data.detail.value;
    let undone_detail = this.data.detail.value;
    let job_isShow = false;
    let undone_isShow = false;
    let job_flag = false
    let undone_flag = false
    let job_delete = false

    await this.addJob(job_id, job_name, job_time, job_date, job_detail, job_isShow, job_flag, job_delete)
      .then(content => {
        console.log('1.0', content);
        wx.showToast({
          title: '添加成功',
          icon: 'success'
        })
      })
      .catch(error => {
        console.log('1.1', error);
      })

    await this.addUndone(undone_id, undone_name, undone_time, undone_date, undone_detail)
      .then(content => {
        console.log('1.0', content);
        wx.showToast({
          title: '添加成功',
          icon: 'success'
        })
      })
      .catch(error => {
        console.log('1.1', error);
      })
      this.handleClear()
  },

  handleClear() {
        this.setData({
           job:'',
           detail:'',
           date:'',
           currentDate:''
        })
},




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})